package lib.bone.oss.service.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.oss.constant.FileResponseCodeConst;
import lib.bone.oss.constant.FileServiceNameConst;
import lib.bone.oss.constant.FileServiceTypeEnum;
import lib.bone.oss.dao.FileDAO;
import lib.bone.oss.domain.entity.FileEntity;
import lib.bone.oss.service.IFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class FileService {
    @Autowired
    FileDAO fileDAO;


    @Autowired
    private java.util.Map<String, IFileService> fileServiceMap;

    /**
     * 获取文件服务实现
     *
     * @param typeEnum
     * @return
     */
    private IFileService getFileService(FileServiceTypeEnum typeEnum) {
        /**
         * 获取文件服务
         */
        String serviceName = typeEnum.getServiceName();
        IFileService fileService = fileServiceMap.get(serviceName);
        if (null == fileService) {
            throw new RuntimeException("未找到文件服务实现类：" + serviceName);
        }
        return fileService;
    }

    public ResponseDTO<FileEntity> fileUpload(MultipartFile file, FileServiceTypeEnum typeEnum, String path, String fileName) {

        if (null == typeEnum) {
            return ResponseDTO.wrap(FileResponseCodeConst.SERVICE_UNAVAILABLE);
        }
        // 获取文件服务
        IFileService fileService = this.getFileService(typeEnum);
        ResponseDTO<FileEntity> response = fileService.fileUpload(file, path, fileName);
        return response;
    }

//    public ResponseDTO<Pathvo> findPath(String entityId, String researchType){
//        Pathvo pathvo = new Pathvo();
//        List<Filevo> filevos = new ArrayList<>();
//        Optional<Object> o = researchServiceCtl.getById(entityId, researchType);
//        if (o.isPresent()){
//            List<NepuResearchFile> nepuResearchFiles = nepuResearchFileService.getList(entityId,researchType);
//            if (nepuResearchFiles.size()!=0){
//                for (NepuResearchFile nepuResearchFile : nepuResearchFiles) {
//                    Filevo filevo = new Filevo();
//                    FileEntity fileEntity = fileDAO.getById(nepuResearchFile.getFileId());
//                    filevo.setFilleName(fileEntity.getFileName());
//                    filevo.setUrl(fileEntity.getFilePath());
//                    filevos.add(filevo);
//                }
//            }
//            pathvo.setPathvos(filevos);
//            pathvo.setSum(filevos.size());
//            return ResponseDTO.succData(pathvo);
//        }
//
//        return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"实体ID不存在！");
//
//    }


    public ResponseEntity<byte[]> downLoadById(String id, HttpServletRequest request) {
        Optional<FileEntity> fileOp = fileDAO.findById(id);
        if (!fileOp.isPresent()) {
            throw new RuntimeException("文件不存在");
        }

        FileEntity file = fileOp.get();
        // 根据文件服务类 获取对应文件服务 查询 url
        IFileService fileService = null;
        if (file.getFileLocation().equals(FileServiceNameConst.LOCAL)) {
            fileService = this.getFileService(FileServiceTypeEnum.LOCAL);
        } else if (file.getFileLocation().equals(FileServiceNameConst.ALI_OSS)) {
            fileService = this.getFileService(FileServiceTypeEnum.ALI_OSS);
        } else if (file.getFileLocation().equals(FileServiceNameConst.QI_NIU_OSS)) {
            fileService = this.getFileService(FileServiceTypeEnum.QI_NIU_OSS);
        }
        if (Validator.isNotEmpty(fileService)) {
            ResponseEntity<byte[]> stream = fileService.fileDownload(file.getFilePath(), file.getFileName(), request);
            return stream;
        }
        return null;
    }

    public List<FileEntity> getFileList(List<String> fileIds) {
        return fileDAO.findByIdIn(fileIds);
    }


    public Optional getFileEntity(String id) {

        Optional<FileEntity> fileEntityOp = fileDAO.findById(id);

        return fileEntityOp;

    }
}
